package com.wisemoresystem.seal.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.page.Page;
import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.util.DateTools;
import com.wisemoresystem.seal.util.json.JsonTools;

@Service
public class SystemLogService extends BaseService {

	@Autowired
	private VerifyService vService;
	@Autowired
	private BankService bService;

	public List<Map<String, Object>> selectVerify(String srh_bank_id,
			String srh_name, String srh_start, String srh_end, Page page) {
		String ids = bService.selectUbank2();;
		if(ChkTools.isNull(ids)){
			ids = "''";
		}
		String sql = "select t.*,t.note vnote,tu.uname operator_name,tb.name bname from t_bank_verify t "
				+ " left join t_user tu on t.submit_user_id=tu._id "
				+ " left outer join t_bank tb on tu.bank_id=tb._id "
				+ " where t.status=0 and t.flag is not null and tb._id in (@ids) and tb._id=? and tu.uname like ? "
				+ " and t.created>=? and t.created<=? "
				+ " order by t.created desc ";
		List<String> pamars = new ArrayList<String>();
		if (ChkTools.isNull(srh_bank_id)) {
			sql = sql.replace("and tb._id=?", "");
		} else {
			pamars.add(srh_bank_id);
		}
		if (ChkTools.isNull(srh_name)) {
			sql = sql.replace("and tu.uname like ?", "");
		} else {
			pamars.add("%" + srh_name + "%");
		}
		if (ChkTools.isNull(srh_start)) {
			sql = sql.replace("and t.created>=? and t.created<=?", "");
		} else {
			pamars.add(srh_start);
			pamars.add(DateTools.addOneDay(srh_end));
		}
		sql = sql.replace("@ids", ids);
		List<Map<String, Object>> list = jc.queryForList(sql, pamars.toArray(),
				page);
		for (Map<String, Object> map : list) {
			if ("机构审核".equals(map.get("flag_type") + "")) {
				String flag_note = map.get("flag_note") + "";
				if ("机构撤销审核".equals(flag_note) || "机构撤销审核".equals(flag_note)) {
					String before = map.get("before_v") + "";
					map = vService.turnToMap(map, before);
				} else {
					String after = map.get("after_v") + "";
					map = vService.turnToMap(map, after);
				}
			} else {
				Map<String, Object> maps = new HashMap<String, Object>();
				String before = (String) map.get("before_v");
				maps = JsonTools.jsonStrToMap(before);

				map.put("u_name", maps.get("uname"));
				map.put("u_code", maps.get("code"));
			}
		}
		return list;
	}
}
